package com.company.OfferGuide;

import com.company.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class 二叉树中和为某一值的路径 {

    List<List<Integer>> lists = new ArrayList<>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if (root == null) return lists;
        helpPath(new ArrayList<>(),root,sum,0);
        return lists;
    }

    public void helpPath(List<Integer> path,TreeNode root,int sum,int cur){
        cur += root.val;
        path.add(root.val);
        if (cur == sum && root.left == null && root.right == null) {
            lists.add(new ArrayList<>(path));
        }
        if (root.left != null) {
            helpPath(path,root.left,sum,cur);
            path.remove(path.size() - 1);
        }
        if (root.right != null) {
            helpPath(path,root.right,sum, cur);
            path.remove(path.size() - 1);
        }
    }


}
